//CC.cpp 
//ColorCoded Main
/*
Nathan Currier / Rideliner
	nathan.currier@gmail.com
	rideliner.uni.cc
ColorCoded
	http://code.google.com/p/colorcoded/
*/

#include <iostream> //cin & cout
#include <fstream> //in/out-put to files
#include <iomanip> //setf & setprecision
#include <string> //string class
#include <sstream> //stringstream
#include <vector> //vector class
#include "Color.h" //Color class
#include "CC.h" //color linker
#include "..\VERS_DECL.h"
#include <ctype.h>
#include <cstdio>
#include <cstdlib>
#include <tchar.h> //_T
#include <windows.h> //windows api
#include <WinInet.h>
#include <urlmon.h>
#pragma comment(lib, "urlmon.lib")
#pragma warning(disable: 4996)
#pragma warning(disable: 4018)
using namespace std;

void get_colors(vector <Color> &colors);
void create_html(vector <Color> colors);
void create_keyfiles();
char menu();
void add_colors();
void open_index();
int check_update();
void color_check(string newname, string newcode);

int main()
{	
	vector <Color> colors;
	char html_opt;
	char choice;
	int update;

	update = check_update();
	
	if(update == 0)
	{
		return 0;
	}

	do
	{
		choice = menu();
		system("cls");
		switch(choice)
		{
			case 'a':
				add_colors();
				break;
			case 'd':
				dl_def_colors();
				break;
			case 'f':
				create_keyfiles();
				break;
			case 'h':
				get_colors(colors);
				create_html(colors);
				create_keyfiles();
				open_index();
				return 0;
				break;
			case 'r':
				create_readme();
				break;
			case 'o':
				open_index();
				break;
			case 'q':
				fstream html;
				html.open("ColorCoded.html", ios::in);
				if(html.good())
				{
					cout << "Would you like to open the HTML index? (Y/N) ";
					cin >> html_opt;
					html_opt = tolower(html_opt);
					if(html_opt == 'y')
					{
						open_index();
					}
				}
				html.close();
				break;
		}
	}
	while(choice != 'q');

	return 0;
}

void dl_def_clrs()
{
	HRESULT hr = URLDownloadToFile ( NULL, _T("http://colorcoded.googlecode.com/svn/trunk/Color.dat"), _T("Color.dat"), 0, NULL );

	system("cls");

	if(hr == 0)
	{
		setrgb(2);
		cout << "Color.dat was downloaded successfully.";
	}
	else
	{
		setrgb(1);
		cout << "Color.dat could not be downloaded, please check your internet connection.";
	}
	setrgb(0);

	cout << "\n\n\n\n";
	system("pause");
}

int check_update()
{
	ifstream version_H;
	ofstream update_dat;
	char dl_continue = NULL;
	char update = NULL;
	string dev_ver1, version1;
	string dev_ver2, version2;

	version_H.open("http://colorcoded.googlecode.com/svn/trunk/VERS_DECL.h", ios::in);
		
	if(version_H.good())
	{
		for(int l = 0; l <= 10; l++)
		{
			getline(version_H, version2);
		}
		getline(version_H, dev_ver2);
		version_H.close();
		version1 = ('\"' + VERSION + '\"');
		dev_ver1 = ('\"' + DEV_VER + '\"');
		version1 = get_vers(true, ".", version1);
		dev_ver1 = get_vers(true, ".", dev_ver1);
		version2 = get_vers(true, ".", version2);
		dev_ver2 = get_vers(true, ".", dev_ver2);
		
		if(version2 != version1)
		{
			cout << "An update, " << version2 << ", is available\n\tCurrent Version is " << version1 << "\n\n";

			while(update != 'n')
			{
				cout << "Y \\ y\tDownload v" << version2 << endl;
				cout << "N \\ n\tDownload Later\n  ";
				cin >> update;

				update = tolower(update);

				if(update == 'y')
				{
					while(dl_continue != 'n')
					{ 
						if(dev_ver2 != dev_ver1)
						{
							rename("CC Dev.exe", "CC Dev2.exe");
							HRESULT hr = URLDownloadToFile(NULL, _T("http://colorcoded.googlecode.com/files/CC Dev.exe"), _T("CC Dev.exe"), 0, NULL);
							
							if(hr == 0)
							{
								remove("CC Dev2.exe");
								system("cls");
								setrgb(2);
								cout << "Updated \'CC Dev.exe\'\n\n\n";
								setrgb(0);

								update_dat.open("Update.dat", ios::out | ios::trunc);
								update_dat << "Update";
								update_dat.close();

								system("pause");
								system("cmd.exe /c start \"\" \"CC Dev.exe\"");

								return 0;
							}
							else
							{
								rename("CC Dev2.exe", "CC Dev.exe");
								setrgb(1);
								cout << "Failed to update \'CC Dev.exe\'" << endl;
								setrgb(0);
								cout << "Do you want to to try downloading the new \'CC Dev.exe\' again? (Y/N) ";
								cin >> dl_continue;
								dl_continue = tolower(dl_continue);
							}
						}
						else
						{
							dl_continue = 'n';
						}
					}
					
					update_dat.open("Update.dat", ios::out | ios::trunc);
					update_dat << "Update";
					update_dat.close();

					system("pause");
					system("cmd.exe /c start \"\" \"CC Dev.exe\"");

					return 0;
				}
				if(update != 'n')
				{
					cout << "\nInvalid Option\n\n";
					system("pause");
				}
				system("cls");
			}
		}
	}
	else
	{
		version_H.close();
	}
	return 1;
}

void open_index()
{
	ShellExecute(NULL, TEXT("open"), TEXT("ColorCoded.html"), NULL, NULL, SW_SHOWNORMAL);
}

void add_colors()
{
	char new_name[50];
	string new_code;
	fstream color_file;
	
	system("cls");
	
	cout << "Name (Q/q to quit): ";
	cin.get(new_name, 50);
	cin.ignore(100, '\n');
	new_name[0] = toupper(new_name[0]);
	
	while(new_name[0] != 'Q' && new_name[1] != '\0')
	{
		cout << "Code of " << new_name << ": ";
		getline(cin, new_code);
		
		color_check(new_name, new_code);
		
		cout << "\nName (Q/q to quit): ";
		cin.get(new_name, 50);
		cin.ignore(100, '\n');
		new_name[0] = toupper(new_name[0]);
	}
}

void color_check(string new_name, string new_code)
{
	string dat_name, dat_code;
	char check_name = 'n';
	char check_code = 'n';
	
	ifstream color_data;
	ofstream newcolorfile;
	
	color_data.open("Color.dat", ios::in);
	
	while(!color_data.eof())
	{
		getline(color_data, dat_name);
		if(!color_data.eof())
		{
			if(dat_name == new_name)
			{
				check_name = 'y';
			}
			
			getline(color_data, dat_code);
			if(dat_code == new_code)
			{
				check_code = 'y';
			}
		}
	}
	
	if(check_name == 'n' && check_code == 'n')
	{
		newcolorfile.open("Color.dat", ios::out | ios::app);
		newcolorfile << new_name << endl << new_code << endl;
		newcolorfile.close();
		setrgb(2);
		cout << new_name << " with code #" << new_code << " was added successfully." << endl;
		setrgb(0);
	}
	else
	{
		setrgb(1);
		if(check_name == 'y' && check_code == 'y')
		{
			cout << "The color: " << new_name << " and code: #" << new_code << " already exist.\n";
		}
		else if(check_name == 'y' && check_code == 'n')
		{
			cout << "The color: " << new_name << " already exists.\n";
		}
		else if(check_name == 'n' && check_code == 'y')
		{
			cout << "The code: #" << new_code << " already exists.\n";
		}
		setrgb(0);
	}
	
	cout << "\n\n\n";
	system("pause");
	system("cls");
}

char menu()
{
	char option;
	int opt_num = 0;	
	bool html_good = false;
	bool btn_good = false;
	bool jquery_good = false;
	bool top_good = false;
	bool clr_good = false;
	string ver_space;
	fstream html, btn, jquery, top, clr;
	
	string version = get_vers();

	string chk_release = get_vers(false);
	char release = 'R';

	if(chk_release != "0")
	{
		release = 'B';
	}

	int ver_len = sizeof(version);

	for(int i = ver_len; i <= 66; i++)
	{
		ver_space += ' ';
	}
	ver_space = ver_space + release + " v" + version;

	html.open("ColorCoded.html", ios::in);
	if(html.good())
	{
		html_good = true;
	}
	html.close();
	btn.open("button_topofpage.js", ios::in);
	if(btn.good())
	{
		btn_good = true;
	}
	btn.close();
	jquery.open("jquery.min.js", ios::in);
	if(jquery.good())
	{
		jquery_good = true;
	}
	jquery.close();
	top.open("Top.png", ios::in);
	if(top.good())
	{
		top_good = true;
	}
	top.close();
	clr.open("Color.dat", ios::in);
	if(clr.good())
	{
		clr_good = true;
	}
	clr.close();
	
	do
	{
		system("cls");
		opt_num++;
		if(opt_num > 1)
		{
			cout << "Invalid Option, please try again...\n\n";
		}
		cout << "A \\ a\tAdd color to Color.dat" << ver_space;
		cout << "D \\ d\tDownload default Color.dat" << endl;
		if(html_good && !(btn_good && jquery_good && top_good))
		{
			cout << "F \\ f\tDownload Keyfiles" << endl;
		}
		if(clr_good)
		{
			cout << "H \\ h\tCreate the HTML file" << endl;
		}
		if(html_good)
		{
			cout << "O \\ o\tOpen Index" << endl;
		}
		cout << "R \\ r\tRead Me" << endl;
		cout << "Q \\ q\tQuit" << endl << "  ";
		cin >> option;
		option = tolower(option);
	}
	while(!((option == 'f' && html_good && !(btn_good && jquery_good && top_good)) || (option == 'h' && clr_good) || (option == 'd') || (option == 'a') || (option == 'r') || (option == 'q') || (option == 'o' && html_good)));

	return option;
}

void get_colors(vector <Color> &colors)
{
	fstream data;
	string name, code;
	Color temp;
	int j;

	data.open("Color.dat", ios::in);
	do
	{
		getline(data, name);
		getline(data, code);
		if(!data.eof())
		{
			temp.set(name, code);
			colors.push_back(temp);
		}
	}
	while(!data.eof());
	data.close();

	for(int i = 1; i < colors.size(); i++)
	{		
		temp = colors[i];
		j = (i - 1);

		while((j >= 0) && (colors[i].get_name() < colors[j].get_name()))
		{
			colors[j + 1] = colors[j];
			j--;
			colors[j + 1] = temp;
		}
	}
}

void create_html(vector <Color> colors)
{
	fstream thefile;
	string name, code, split;

	thefile.open("ColorCoded.html", ios::out | ios::trunc);

	thefile << "<!DOCTYPE HTML><HTML>" << endl;
	thefile << "<HEAD>" << endl;
	thefile << "<TITLE>ColorCoded</TITLE>" << endl;
	thefile << "<script type=\"text/javascript\" src=\"jquery.min.js\"></script>" << endl;
	thefile << "<script type=\"text/javascript\" src=\"button_topofpage.js\"></script>" << endl;
	thefile << "<style type=\"text/css\">" << endl;
	thefile << "body {font-weight:bold}" << endl;
	thefile << ".fgclr {text-align:right; width:10%;}" << endl;
	thefile << ".bgclr {text-align:center; width:80%;}" << endl;
	thefile << ".cdclr {text-align:left; width:10%; font-family:\"courier new\";}" << endl;
	thefile << "a {text-decoration:none;}" << endl;
	thefile << "a:link {color:#000000;}" << endl;
	thefile << "a:visited {color:#000000;}" << endl;
	thefile << "a:hover {color:#000000;}" << endl;
	thefile << "a:active {color:#000000;}" << endl;
	thefile << "</style>" << endl;
	thefile << "<script type=\"text/javascript\">" << endl;
	thefile << "var browser=navigator.appName;" << endl;
	thefile << "if(browser==\"Microsoft Internet Explorer\")" << endl;
	thefile << "{" << endl;
	thefile << "\talert(\"Some scripts on this page will not work properly... Try Firefox or Chrome for full functionality.\");" << endl;
	thefile << "}" << endl;
	thefile << "var bgcolor;" << endl;
	thefile << "var fgcolor;" << endl << endl;

	thefile << "$(document).ready(function()" << endl;
	thefile << "{" << endl;
	thefile << "\t$('TR').click(function()" << endl;
	thefile << "\t{" << endl;
	thefile << "\t\tbgcolor = $(this).attr('class');" << endl;
	thefile << "var name = $(this).attr('id');" << endl;
	thefile << "\t\tvar code = bgcolor.split(\" \", 3);" << endl;
	thefile << "\t\tfor(var t = 0; t <= 2; t = t + 1)" << endl;
	thefile << "\t\t{" << endl;
	thefile << "\t\tcode[t] = (255 - parseInt(code[t], 16));" << endl;
	thefile << "\t\t}" << endl;
	thefile << "\t\tfor(var i = 0; i <= 2; i = i + 1)" << endl;
	thefile << "\t\t{" << endl;
	thefile << "\t\t\tcode[i] = code[i].toString(16);" << endl;
	thefile << "\t\t\tif(code[i].length == 1)" << endl;
	thefile << "\t\t\t{" << endl;
	thefile << "\t\t\t\tcode[i] = 0 + code[i];" << endl;
	thefile << "\t\t\t}" << endl;
	thefile << "\t\t}" << endl;
	thefile << "\t\tfgcolor = code.join(\"\");" << endl << endl;
		
	thefile << "\t\tbgcolor = bgcolor.split(\" \", 3);" << endl;
	thefile << "\t\tbgcolor = bgcolor.join(\"\");" << endl << endl;

	thefile << "\t\tdocument.title = (name + ' - #' + bgcolor);" << endl << endl;
				
	thefile << "\t\t$('body').css('background-color','#'+bgcolor);" << endl;
	thefile << "\t\t$('.bgclr').css('color','#'+bgcolor);" << endl;
	thefile << "\t\t$('.footer').css('color','#'+fgcolor);" << endl;
	thefile << "\t\t$('a:link').css('color','#'+fgcolor);" << endl;
	thefile << "\t\t$('a:visited').css('color','#'+fgcolor);" << endl;
	thefile << "\t\t$('a:hover').css('color','#'+fgcolor);" << endl;
	thefile << "\t\t$('a:active').css('color','#'+fgcolor);" << endl;
	thefile << "\t\t$('.header').css('color','#'+fgcolor);" << endl;
	thefile << "\t\t$('.cdclr').css('color','#'+fgcolor);" << endl;
	thefile << "\t});" << endl;
	thefile << "});" << endl;
	thefile << "</script>" << endl;
	thefile << "</HEAD>" << endl << endl;
	
	thefile << "<BODY>" << endl;
	thefile << "\t<div class=\"header\">" << endl;
	thefile << "\t\t<CENTER><FONT SIZE=\"+2\">Please choose a background color:</FONT>" << endl;
	thefile << "\t</div>" << endl;
	thefile << "<HR>" << endl;
	thefile << "<BR>" << endl << endl;

	thefile << "<center>" << endl;
	thefile << "<TABLE BORDER=\"0\" WIDTH=\"80%\" cellpadding=\"0\" cellspacing=\"0\">" << endl << endl << endl;
	
	for(int h = 0; h < colors.size(); h++)
	{
		colors[h].get_back(name, code);
		split = colors[h].get_split();

		thefile << "<TR id=\"" << name << "\" class=\"" << split << "\">" << endl;
		thefile << "\t<TD class=\"fgclr\"><FONT COLOR=\"#" << code << "\">" << name << "</FONT></TD>" << endl;
		thefile << "\t<TD class=\"bgclr\" BGCOLOR=\"#" << code << "\">" << name << "</TD>" << endl;
		thefile << "\t<TD class=\"cdclr\">#" << code << "</TD>" << endl;
		thefile << "</TR>" << endl << endl;
	}

	thefile << "\n</TABLE>" << endl;
	thefile << "&nbsp;" << endl;
	thefile << "</BODY>" << endl;
	thefile << "<footer class=\"footer\">" << endl;
	thefile << "<HR" << endl;
	thefile << "<center>Page compiled by: <a href=\"http://code.google.com/p/colorcoded/\" target=\"_blank\">Nathan Currier</a>" << endl;
	thefile << "</footer>"; 

	thefile.close();
}
